package com.edu.edu.week_project01.activity.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.edu.edu.week_project01.R;
import com.edu.edu.week_project01.activity.FactoryFragment.FactoryFragment;
import com.edu.edu.week_project01.activity.adapter.MyPageAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 10/11 0011.
 */
public class LiveDetailActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager mVp;
    private TabLayout mTab;
    private Button bt_focus;
    private Button bt_hot;
    private Button bt_send;
    private VideoView mVideoView;
    private EditText et_content;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
//        setContentView(R.layout.live_detail_list_item);
        //初始化控件
        initView();
        //tablayout与fragment的监听
//        tabFragmentListener();
        //toolbar的监听
        toolbarListener();
    }
    //toolbar的监听
    private void toolbarListener() {
        final ArrayAdapter dialogAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        String[] reportContent = getResources().getStringArray(R.array.report_content);
        for (int i = 0; i < reportContent.length; i++) {
            dialogAdapter.add(reportContent[i]);
        }
        //填充菜单
        mToolbar.inflateMenu(R.menu.over);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action_report://举报
                        Toast.makeText(LiveDetailActivity.this,"举报", Toast.LENGTH_LONG).show();

                        break;
                    case R.id.action_shape://分享
                        Toast.makeText(LiveDetailActivity.this,"分享", Toast.LENGTH_LONG).show();

                        break;
                }
                return false;
            }
        });
    }
    private void tabFragmentListener() {
        String[] ZTitles = getResources().getStringArray(R.array.live_detail);
        List<String> titles = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < ZTitles.length; i++) {
            Fragment fragment = FactoryFragment.createFragment(i);
            mTab.getTabAt(i);
            titles.add(ZTitles[i]);
            fragments.add(fragment);
        }
        //创建适配器
        final MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(),fragments,titles);
        assert adapter != null;
        //设置viewpager适配器
        mVp.setAdapter(adapter);
        //联动监听
        mTab.setupWithViewPager(mVp);
    }

    private void initView() {
        mTab = (TabLayout) findViewById(R.id.live_detail_tablayout);
        mToolbar = (Toolbar) findViewById(R.id.live_detail_toolbar);
        mVp = (ViewPager) findViewById(R.id.live_detail_viewpager);
        mVideoView = (VideoView) findViewById(R.id.live_detail_Videoview);
        et_content = (EditText) findViewById(R.id.live_detail_etContent);
        ImageView iv_avatar = (ImageView) findViewById(R.id.total_iv_thumb);
        bt_focus = (Button) findViewById(R.id.live_detail_bt_focus);
        bt_hot = (Button) findViewById(R.id.live_detail_bt_hot);
        bt_send = (Button) findViewById(R.id.live_detail_bt_send);
        bt_focus.setOnClickListener(this);
        bt_hot.setOnClickListener(this);
        bt_send.setOnClickListener(this);
    }
    //播放视频
    public void startVideo(String path){
        mVideoView.setVideoURI(Uri.parse(path));
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.start();

    }

    //按钮点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.live_detail_bt_focus://关注
                Toast.makeText(LiveDetailActivity.this,"关注", Toast.LENGTH_LONG).show();
                break;
            case R.id.live_detail_bt_hot://热
                Toast.makeText(LiveDetailActivity.this,"热", Toast.LENGTH_LONG).show();
                break;
            case R.id.live_detail_bt_send://发送
                Toast.makeText(LiveDetailActivity.this,"发送", Toast.LENGTH_LONG).show();
                break;
        }
    }
}
